{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:03:42.177364Z",
     "start_time": "2025-04-27T12:03:42.170737Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torch import nn\n",
    "\n",
    "\n",
    "class CenteredLayer(nn.Module):\n",
    "\tdef __init__(self):\n",
    "\t\tsuper().__init__()\n",
    "\n",
    "\tdef forward(self, X):\n",
    "\t\treturn X - X.mean()\n",
    "\n",
    "\n",
    "layer = CenteredLayer()\n",
    "layer(torch.FloatTensor([1, 2, 3, 4, 5]))"
   ],
   "id": "3ea521843ce12436",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-2., -1.,  0.,  1.,  2.])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:03:42.208658Z",
     "start_time": "2025-04-27T12:03:42.194746Z"
    }
   },
   "cell_type": "code",
   "source": "net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())",
   "id": "e59cde66e53319a8",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:03:50.238991Z",
     "start_time": "2025-04-27T12:03:50.228833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "Y = net(torch.rand(4, 8))\n",
    "Y.mean()"
   ],
   "id": "ae1de31a4a1801a4",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(4.6566e-10, grad_fn=<MeanBackward0>)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:04:29.046883Z",
     "start_time": "2025-04-27T12:04:29.035614Z"
    }
   },
   "cell_type": "code",
   "source": [
    "class MyLinear(nn.Module):\n",
    "    def __init__(self, in_units, units):\n",
    "        super().__init__()\n",
    "        self.weight = nn.Parameter(torch.randn(in_units, units))\n",
    "        self.bias = nn.Parameter(torch.randn(units,))\n",
    "    def forward(self, X):\n",
    "        linear = torch.matmul(X, self.weight.data) + self.bias.data\n",
    "        return F.relu(linear)"
   ],
   "id": "8b244e9e9462b55b",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:04:30.326584Z",
     "start_time": "2025-04-27T12:04:30.313532Z"
    }
   },
   "cell_type": "code",
   "source": [
    "linear = MyLinear(5, 3)\n",
    "linear.weight"
   ],
   "id": "e41d86b7df19f5ea",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Parameter containing:\n",
       "tensor([[ 0.2225,  1.4611, -1.3869],\n",
       "        [ 0.3130, -0.0433, -0.1212],\n",
       "        [ 0.1254,  0.6003, -1.5732],\n",
       "        [-0.9565, -0.4432,  0.6157],\n",
       "        [ 0.3880, -0.5130,  0.5725]], requires_grad=True)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:05:44.438688Z",
     "start_time": "2025-04-27T12:05:44.425713Z"
    }
   },
   "cell_type": "code",
   "source": "linear(torch.rand(2, 5))",
   "id": "826a8dd2d80ae87b",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.1232, 1.4303, 0.7786],\n",
       "        [1.0023, 1.7188, 0.1923]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-27T12:05:58.250489Z",
     "start_time": "2025-04-27T12:05:58.238501Z"
    }
   },
   "cell_type": "code",
   "source": [
    "net = nn.Sequential(MyLinear(64, 8), MyLinear(8, 1))\n",
    "net(torch.rand(2, 64))"
   ],
   "id": "90fa20f296f83375",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1.3924],\n",
       "        [0.0000]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "58f005db636a988b"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
